﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Reflection;
using System.Data;
using System.Xml;
using System.IO;
using System.Text;

namespace FancyIdea.PingShui.Core.Util
{
    public class XmlUtil
    {
        private static string xmlPath = (System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.ToString())
            + @"\xml\").Substring(6);

        public static void WriteXml(DataTable table, string fileName)
        {
            if (table == null || table.Rows.Count <= 0) return;
            DirectoryInfo dir = new DirectoryInfo(xmlPath);
            if (!dir.Exists) dir.Create();
            XmlTextWriter writer = new XmlTextWriter(xmlPath+fileName, null);
            writer.Formatting = Formatting.Indented;  //缩进格式
            writer.Indentation = 4;
            writer.WriteStartDocument();
            writer.WriteStartElement("Datas");
            for (int i = 0; i < table.Rows.Count; i++)
            {
                writer.WriteStartElement("Data");
                for (int j = 0; j < table.Columns.Count; j++)
                {
                    writer.WriteElementString(table.Columns[j].ColumnName, table.Rows[i][j].ToString());
                }
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
        }

        public static void WriteReturnXml(int code, string message, DataTable table, string fileName)
        {
            DirectoryInfo dir = new DirectoryInfo(xmlPath);
            if (!dir.Exists) dir.Create();
            XmlTextWriter writer = new XmlTextWriter(xmlPath + fileName, null);
            writer.Formatting = Formatting.Indented;  //缩进格式
            writer.Indentation = 4;
            writer.WriteStartDocument();
            writer.WriteStartElement("response");

            writer.WriteElementString("code",code.ToString());
            writer.WriteElementString("message", message);
            writer.WriteStartElement("datas");
            if (table != null && table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    writer.WriteStartElement("data");
                    for (int j = 0; j < table.Columns.Count; j++)
                    {
                        writer.WriteElementString(table.Columns[j].ColumnName, table.Rows[i][j].ToString());
                    }
                    writer.WriteEndElement();
                }
            }
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
        }

        public static void WriteListXml(int code,string message,
            int count, int perPage, int total,int page,
            DataTable table, string fileName)
        {
            DirectoryInfo dir = new DirectoryInfo(xmlPath);
            if (!dir.Exists) dir.Create();
            XmlTextWriter writer = new XmlTextWriter(xmlPath + fileName, null);
            writer.Formatting = Formatting.Indented;  //缩进格式
            writer.Indentation = 4;
            writer.WriteStartDocument();
            writer.WriteStartElement("response");
            writer.WriteElementString("code", code.ToString());
            writer.WriteElementString("message", message);

            writer.WriteStartElement("data");
            writer.WriteElementString("count", count.ToString());
            writer.WriteStartElement("page_info");
            writer.WriteElementString("per_page", perPage.ToString());
            writer.WriteElementString("total", total.ToString());
            writer.WriteElementString("page", page.ToString());
            writer.WriteEndElement();
            writer.WriteStartElement("datas");
            if (table != null && table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    writer.WriteStartElement("data");
                    for (int j = 0; j < table.Columns.Count; j++)
                    {
                        writer.WriteElementString(table.Columns[j].ColumnName, table.Rows[i][j].ToString());
                    }
                    writer.WriteEndElement();
                }
            }
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
        }

        public static DataTable ReadXml(string fileName)
        {
            DataSet ds = new DataSet();
            DataTable table = new DataTable();
            if (File.Exists(xmlPath + fileName))
            {
                ds.ReadXml(xmlPath + fileName);
                table = ds.Tables[0].Clone();
                table = ds.Tables[0];
                return table;
            }
            else
            {
                return null;
            }
        }
    }
}